import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../../module_common/provider/wallet_info_provider.dart';
import '../../../../module_common/router/routers.dart';
import '../../../../module_common/utils/auth.dart';
import '../../../../module_common/widgets/button/bottom_button.dart';
import '../../../../module_common/widgets/dialog/bottom_sheet_widget.dart';
import '../../../../module_common/widgets/public_tip/public_tip.dart';

//删除钱包弹窗
class WalletDeleteSheet extends StatefulWidget {
  const WalletDeleteSheet({Key? key}) : super(key: key);

  @override
  _WalletDeleteSheetState createState() => _WalletDeleteSheetState();
}

class _WalletDeleteSheetState extends State<WalletDeleteSheet> {
  List<bool> _isCheckArray = [false, false, false];
  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
        title: '',
        expand: false,
        child: Container(
          padding: EdgeInsets.only(left: 20.w, right: 20.w),
          child: Column(
            children: [
              Row(
                children: [
                  Image.asset(
                    'assets/images/status/warn.png',
                    width: 20.w,
                    height: 20.w,
                  ),
                  SizedBox(width: 7.w),
                  Text("Safety Warning",
                      style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold, color: "#333333".toColor()))
                ],
              ),
              Text(
                'Are you sure you want to delete this wallet? please back up the mnemonic phrase and private key, as you can recover the wallet using either.',
                style: TextStyle(color: "#868391".toColor(), fontSize: 12.sp),
              ),
              SizedBox(height: 15.w),
              checkBox(
                  text: 'Remove data for the wallet on this device.',
                  isCheck: _isCheckArray[0],
                  onChanged: (value) {
                    setState(() {
                      _isCheckArray[0] = value;
                    });
                  }),
              SizedBox(height: 10.w),
              checkBox(
                  text:
                      'You have already backed up the mnemonic phrases and private keys for all walle taccounts, and you can recover the wallet using either.',
                  isCheck: _isCheckArray[1],
                  onChanged: (value) {
                    setState(() {
                      _isCheckArray[1] = value;
                    });
                  }),
              SizedBox(height: 10.w),
              checkBox(
                  text:
                      'Kai does not hold your mnemonic phrases and private keys and cannot help you recover the wallet.',
                  isCheck: _isCheckArray[2],
                  onChanged: (value) {
                    setState(() {
                      _isCheckArray[2] = value;
                    });
                  }),
              SizedBox(height: 20.w),
              BottomBtn(
                  textList: ["Cancel", 'Confirm'],
                  disable: !(_isCheckArray[0] && _isCheckArray[1] && _isCheckArray[2]),
                  onCancel: () {
                    Navigator.pop(context);
                  },
                  onConfirm: () {
                    //内容校验

                    Authentication()
                        .startLocalAuth(
                            showPasswordError: false,
                            callBackError: () {
                              Toast.show(context,
                                  status: '0',
                                  position: Dpositon.top,
                                  message: "Verification password error",
                                  duration: Duration(milliseconds: 3000));
                            })
                        .then((result) {
                      if (result) {
                        deleteWallet();
                        Toast.show(context, message: 'Operating succeeded').then((value) {
                          var walletList = ObjectBox().walletModel.getSortedWalletList();
                          if (walletList.isEmpty) {
                            //如果清空了就直接返回空钱包页面
                            Routes.navigateTo(context, Routes.rootRoute,
                                replace: true, transition: TransitionType.inFromLeft);
                          } else {
                            //
                            Routes.navigateTo(context, Routes.walletExportPrivateKey);
                          }
                        });
                      } else {
                        Toast.show(context, status: '2', message: 'Operation failed');
                      }
                    });
                  }),
              SizedBox(
                height: (MediaQuery.of(context).padding.bottom + 10).w,
              )
            ],
          ),
        ));
  }

  Widget checkBox({required String text, required bool isCheck, required void Function(bool) onChanged}) {
    return Container(
      padding: EdgeInsets.only(left: 15.w, top: 15.w, bottom: 15.w),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(10.w), color: '#F6F6F6'.toColor()),
      child: Row(
        children: [
          Expanded(child: Text(text, style: TextStyle(color: "#868391".toColor(), fontSize: 12.sp))),
          InkWell(
            child: Container(
              padding: EdgeInsets.only(left: 5.w, top: 10.w, bottom: 10.w, right: 16.w),
              child: Image.asset(
                  isCheck ? 'assets/images/wallet/delete_check.png' : 'assets/images/wallet/delete_uncheck.png',
                  width: 16.w,
                  height: 16.w),
            ),
            onTap: () {
              isCheck = !isCheck;
              //改变颜色

              onChanged(isCheck);
            },
          )
        ],
      ),
    );
  }

  void deleteWallet() {
    int id = context.read<WalletInfoNotifier>().walletId;
    if (id != 0) {
      ObjectBox().deleteWallet(id);
    }
  }
}
